//
//  HandleHistoryViewController.m
//  Utils
//
//  Created by enghou on 2018/4/19.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "HandleHistoryViewController.h"
#import "HandleHistoryItemModel.h"
#import "MSSBrowseNetworkViewController.h"
#import <AVKit/AVKit.h>
#import "PlayAudioViewController.h"
@interface HandleHistoryViewController ()

@end

@implementation HandleHistoryViewController

-(NSDictionary *)param{
    if (self.type == 0) {
        return @{@"id":self.event_id};
    }else{
        return @{@"appealId":self.event_id};
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"办理过程";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleWithObject:) name:@"ShowWithObject" object:nil];
}

-(void)refreshSuccessWithModel:(BaseModel *)model{
    self.source = [model.list mutableCopy];
    if ([self.source count] > 0) {
        HandleHistoryItemModel *first = self.source.firstObject;
        HandleHistoryItemModel *last = self.source.lastObject;
        
        if ([first isEqual:last]) {
            first.hasPrevious = NO;
            first.hasNext = NO;
            first.current = YES;
        }else{
            first.hasPrevious = YES;
            first.hasNext = NO;
            first.current = YES;
            [self.source enumerateObjectsUsingBlock:^(HandleHistoryItemModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (![obj isEqual:first] && ![obj isEqual:last]) {
                    obj.hasPrevious = YES;
                    obj.hasNext = YES;
                    obj.current = NO;
                }
            }];
            last.hasPrevious = NO;
            last.hasNext = YES;
        }
        
    }
    [self.tableView reloadData];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
}

-(void)handleWithObject:(NSNotification *)not{
    FileItemModel *model = not.object;
    if (model.type == Document) {
        [self showFileWithModel:model];
    }else if (model.type == Video){
        [self showVideoWithModel:model];
    }else if (model.type == Audio){
        [self showAudioWithModel:model];
    }else if (model.type == Image){
        [self showImageWithModel:model];
    }
}

-(void)showImageWithModel:(FileItemModel *)model{
    NSMutableArray *browseItemArray = [[NSMutableArray alloc]init];
    MSSBrowseModel *browseItem = [[MSSBrowseModel alloc]init];
    browseItem.bigImageUrl = [NSString stringWithFormat:@"%@/%@",ServerPath,model.path];
    [browseItemArray addObject:browseItem];
    MSSBrowseNetworkViewController *bvc = [[MSSBrowseNetworkViewController alloc]initWithBrowseItemArray:browseItemArray currentIndex:0];
    bvc.presentVC = self.navigationController;
    [bvc showBrowseViewController];
}

-(void)showVideoWithModel:(FileItemModel *)model{
    AVPlayerViewController *VC = [[AVPlayerViewController alloc]init];
    VC.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,model.path]]];
    VC.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.navigationController presentViewController:VC animated:NO completion:nil];
}

-(void)showAudioWithModel:(FileItemModel *)model{
        PlayAudioViewController *play = [[PlayAudioViewController alloc]init];
        play.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,model.path]];//model.path;
        self.definesPresentationContext = YES;
        play.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        play.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:play animated:NO completion:nil];
}

-(void)showFileWithModel:(FileItemModel *)model{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
